package com.unistec.fileserver.client.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.unistec.fileserver.client.entity.Resource;
import com.unistec.framework.utils.ReturnResult;

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;

/**
 * 上传Feign接口类
 * 
 * @description
 * @author 胡晓光
 * @date 2021-9-28
 * @time 11:05:41
 *
 */
@FeignClient(name = "fileserver", path = "/manager", configuration = IFileServerFeign.MultipartSupportConfig.class)
public interface IFileServerFeign {

	/**
	 * 删除文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 14:43:26
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteFile", method = RequestMethod.POST)
	public JSONObject deleteFile(@RequestParam(name = "id") String id);

	/**
	 * 上传临时文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-28
	 * @time 11:04:41
	 * 
	 * @param file        需要上传的文件
	 * @param asyncUpload 是否异步上传
	 * @param uploadId    上传唯一ID
	 * @return
	 */
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public JSONObject uploadFile(@RequestPart("file") MultipartFile file, @RequestParam(name = "asyncUpload", required = false) Boolean asyncUpload, @RequestParam(name = "folder", required = false) String folder,
			@RequestParam(name = "fileName", required = false) String fileName, @RequestParam(name = "isTempFile", required = false) Boolean isTempFile,
			@RequestParam(name = "uploadId", required = false) String uploadId);

	/**
	 * 通过唯一ID获取资源
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-28
	 * @time 11:05:23
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/getResourceInfoByUnique")
	public ReturnResult<Resource> getResourceInfoByUnique(@RequestParam("unique") String unique);

	/**
	 * 下载文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-30
	 * @time 11:05:08
	 * 
	 * @param unique
	 * @return
	 */
	@RequestMapping(value = "/download", method = RequestMethod.POST)
	public feign.Response download(@RequestParam("unique") String unique);

	class MultipartSupportConfig {
		@Bean
		@Primary
		@Scope("prototype")
		public Encoder multipartFormEncoder() {
			return new SpringFormEncoder();
		}

		@Bean
		public feign.Logger.Level multipartLoggerLevel() {
			return feign.Logger.Level.FULL;
		}
	}
}
